﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocialFront.Entities;
using SocialFront.Providers.Avatar;
using SocialFront.Providers.Member;

namespace SocialFront.Provider.Gravatar
{
    public class GravatarAvatar : AvatarBase
    {
        /// <summary>
        /// Base URL for the Gravatar image
        /// </summary>
        private string BaseURL = "http://www.gravatar.com/avatar/{0}?d={1}&s={2}&r={3}";

        /// <summary>
        /// Rating Property
        /// </summary>
        public enum Rating
        {
            g, pg, r, x
        }

        /// <summary>
        /// Icon Set Property
        /// </summary>
        public enum IconSet
        {
            identicon, monsterid, wavatar
        }

        /// <summary>
        /// Gets the avatar.
        /// </summary>
        /// <param name="member">The member.</param>
        /// <returns></returns>
        public override Entities.Avatar GetAvatar(Entities.Member member)
        {
            return GetAvatar(member, 80);
        }

        /// <summary>
        /// Gets the avatar.
        /// </summary>
        /// <param name="memberId">The member id.</param>
        /// <returns></returns>
        public override Entities.Avatar GetAvatar(int memberId)
        {
            return GetAvatar(MemberProvider.Provider.GetMemberById(memberId));
        }

        /// <summary>
        /// Gets the avatar.
        /// </summary>
        /// <param name="member">The member.</param>
        /// <param name="dimensions">The dimensions.</param>
        /// <returns></returns>
        public override Avatar GetAvatar(Member member, int dimensions)
        {
            Avatar avatar = new Avatar();

            //hash the email address
            string hE = MD5(member.Email);
            //format our url to the Gravatar
            avatar.ImageUrl = string.Format(BaseURL, hE, IconSet.wavatar, dimensions, Rating.x);
            avatar.Width = dimensions;
            avatar.Height = dimensions;
            avatar.Member = member;

            return avatar;
        }

        /// <summary>
        /// Gets the avatar.
        /// </summary>
        /// <param name="MemberId">The member id.</param>
        /// <param name="dimensions">The dimensions.</param>
        /// <returns></returns>
        public override Avatar GetAvatar(int MemberId, int dimensions)
        {
            return GetAvatar(MemberProvider.Provider.GetMemberById(MemberId), dimensions);
        }

        /// <summary>
        /// Small MD5 Function
        /// </summary>
        /// <param name="theEmail">The email.</param>
        /// <returns>Hash of the email address passed.</returns>
        public string MD5(string theEmail)
        {
            if(string.IsNullOrEmpty(theEmail))
                throw new Exception("Email is empty string!");
            System.Security.Cryptography.MD5CryptoServiceProvider md5Obj =
                new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytesToHash = Encoding.ASCII.GetBytes(theEmail);
            bytesToHash = md5Obj.ComputeHash(bytesToHash);
            string strResult = "";
            foreach (byte b in bytesToHash)
            {
                strResult += b.ToString("x2");
            }
            return strResult;
        }

    }
}
